<template>
  <el-container class="main_con">
    <div class="filter_con ds_block">
      <div class="button_area">
        <el-button v-has type="primary" size="medium" @click="addHandle">新增角色</el-button>
      </div>
      <div class="filter_area">
        <el-input
          v-model="params.name"
          class="serach_input mr_15 mt_15"
          size="medium"
          placeholder="用户姓名"
        ></el-input>
        <date-picker size="medium" @change="gettime" class="mr_15 mt_15"></date-picker>
        <el-button size="medium" @click="search">搜索</el-button>
      </div>
    </div>
    <div class="list_con">
      <el-table
        @selection-change="SelectHadnle"
        :data="list"
        element-loading-text
        fit
        highlight-current-row
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="name" label="角色名称"></el-table-column>
        <el-table-column prop="mobile" label="包含用户数"></el-table-column>
        <el-table-column prop="status_text" label="状态"></el-table-column>
        <el-table-column prop="create_time" label="创建时间"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              v-has
              v-if="scope.row.is_root!=1"
              type="text"
              @click="EditHandle(scope.row.id)"
            >编辑</el-button>
            <el-button
              v-has
              v-if="scope.row.is_root!=1"
              type="text"
              @click="EnableHandle(scope.row)"
            >{{scope.row.status==1 ? '禁用' : '启用'}}</el-button>
            <el-button
              v-has
              v-if="scope.row.is_root!=1"
              type="text"
              @click="handleClose(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="table_footer">
        <el-button size="medium">批量删除</el-button>
        <el-pagination
          @current-change="ChangeCurrent"
          class="pages"
          background
          layout="prev, pager, next"
          :total="total"
          :page-count="page"
          :page-size="size"
          hide-on-single-page
        ></el-pagination>
      </div>
    </div>
    <el-dialog
      :title="role_type=='add'?'新增角色' :'编辑角色'"
      :visible.sync="drawer"
      destroy-on-close
      width="800px"
    >
      <edtirole ref="edtirole" @on-close="beClose" :rid="role_id" :role_type="role_type"></edtirole>
    </el-dialog>
  </el-container>
</template>

<script>
import Edtirole from "./components/Edtirole";
import DatePicker from "@/components/DatePicker/index";
import { fitter_keyword } from "@/utils/index";

import { getrolelist, delrole, editrole } from "@/api/mall/role";
export default {
  data() {
    return {
      drawer: false,
      direction: "rtl",
      role_id: "",
      role_type: "add",
      params: {
        name: "",

        begin_date: "",
        end_date: ""
      },
      paramsdf: {},
      page: 1,
      size: 10,
      total: 0,
      listLoading: true,
      list: []
    };
  },
  components: {
    Edtirole,
    DatePicker
  },
  mounted() {
    this.getlist();
  },
  methods: {
    search() {
      this.paramsdf = fitter_keyword(this.params);
      this.getlist();
    },
    getlist() {
      this.listLoading = true;
      let data = this.params;
      getrolelist(this.page, data).then(res => {
        let jsondata = res.msg;
        this.list = jsondata.data;
        this.total = jsondata.total;
        // this.list = response.data.items
        this.listLoading = false;
      });
    },
    //启用，禁用
    EnableHandle(data) {
      let _this = this;
      let status = Number(data.status != 1);
      this.$confirm(status ? "确认启用？" : "确认禁用？")
        .then(_ => {
          let params = {
            id: data.id,
            update_time: data.update_time,
            status: status,
            rule: data.rule
          };
          editrole(params).then(res => {
            _this.getlist();
          });
        })
        .catch(_ => {});
    },
    //选择
    SelectHadnle(selection) {
      this.selectionData = selection;
    },
    //删除
    handleClose(id) {
      let _this = this;
      this.$confirm("确认删除？")
        .then(_ => {
          let data = {
            id: id
          };
          delrole(data).then(res => {
            _this.getlist();
            // _this.$store.dispatch("common/getallroleslist");
          });
        })
        .catch(_ => {});
    },
    //新增
    addHandle() {
      this.role_type = "add";
      this.drawer = true;
      this.role_id = -1;
    },
    //编辑
    EditHandle(id) {
      this.role_id = id;
      this.role_type = "edit";
      this.drawer = true;
      this.$nextTick(() => {
        this.$refs.edtirole.getroledetail(id);
      });
    },
    gettime(value) {
      this.params.begin_date = value[0];
      this.params.end_date = value[1];
    },
    //切换页码
    ChangeCurrent(page) {
      this.page = page;
      this.getlist();
    },
    beClose() {
      this.drawer = false;
      // this.$store.dispatch("common/getallroleslist");
      this.getlist();
    }
  }
};
</script>

<style>
</style>
